package com.haredot.controller;

import com.haredot.service.FuncService;
import com.haredot.utils.ValidUtils;
import com.haredot.vo.FuncVo;
import com.haredot.vo.PageVo;
import com.haredot.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/func")
public class FuncController {

    @Resource
    private FuncService funcService ;

    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('func', 'func:query')")
    public R<PageVo> listToPage(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                @RequestParam(value = "rows", required = false, defaultValue = "10")Integer rows ,
                                @RequestParam(value = "funcName", required = false) String funcName,
                                @RequestParam(value = "menuName", required = false)String menuName) {

        PageVo pageVo = new PageVo(page, rows);
        Map<String, Object> params = new HashMap<>();
        params.put("funcName", funcName);
        params.put("menuName", menuName);
        // 分页带条件查询 功能
        return funcService.queryFuncsByCondition(pageVo, params);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('func:add')")
    public R<?> addFunc(@RequestBody @Validated FuncVo funcVo, BindingResult bindingResult) {

        return ValidUtils.toResult(bindingResult, ()-> {
            return funcService.saveFunc(funcVo) ;
        });
    }

    @PutMapping("/update/{id}")
    @PreAuthorize("hasAuthority('func:update')")
    public R<?> addFunc(@PathVariable("id")Long id, @RequestBody @Validated FuncVo funcVo, BindingResult bindingResult) {

        return ValidUtils.toResult(bindingResult, ()-> {
            funcVo.setId(id);
            return funcService.updateFunc(funcVo) ;
        });
    }

    /**
     * 查询所有的菜单信息 和 功能对应的关系关系
     * @param funcId
     * @return
     */
    @GetMapping("/queryMenusRefFunc")
    @PreAuthorize("hasAnyAuthority('func:update', 'func:add')")
    public R<?> queryMenusRefFunc(@RequestParam(value = "funcId", required = false) Long funcId) {
        return funcService.queryMenusRefFunc(funcId);
    }

    @DeleteMapping("/batchDelete")
    @PreAuthorize("hasAuthority('func:batch:del')")
    public  R<?> batchDelete(@RequestBody List<Long> funcIds) {
        if (funcIds.isEmpty()) {
            return R.fail("参数校验错误").data(Map.of("funcIds", "请至少选择一个需要删除的功能"));
        }
        return funcService.batchDelete(funcIds) ;
    }

    @DeleteMapping("/del/{funcId}")
    @PreAuthorize("hasAuthority('func:delete')")
    public R<?> deleteFunc(@PathVariable("funcId") Long funcId) {

        return funcService.removeFunc(funcId);
    }
}
